package com.example.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.domain.Goods;
import com.example.domain.paging.PageHelper;
import com.example.service.IGoodsService;
import com.example.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.Goods;
import com.example.domain.User;
import com.example.service.IGoodsService;
import com.example.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理接口")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    //添加商品
    @ApiOperation("添加商品")
    @PostMapping("/addGoods")
    @PreAuthorize("hasAnyAuthority('supplier')")
    public Result addGoods(@RequestBody Goods goods){

        Result result =new Result();
        Goods one = goodsService.getOne(new QueryWrapper<Goods>().eq("name", goods.getName()).eq("uid", goods.getUid()));
        if(one!=null){
            result.setFlag(true);
            result.setData("该商品已存在");
        }else{
            if(goods.getName()!=null&&goods.getStock()!=null
                    &&goods.getClassification()!=null&&goods.getUid()!=null&&goods.getPrice()!=null){
                goodsService.addGoods(goods);
                result.setFlag(true);
                result.setData("商品添加成功");
            }else{
                result.setFlag(true);
                result.setData("部分内容未填写");
            }
        }
        return result;
    }

    //删除商品
    @ApiOperation("删除商品")
    @PostMapping("/deleteGoods")
    @PreAuthorize("hasAnyAuthority('supplier')")
    public Result deleteGoods(@RequestBody Goods good){
        Result result =new Result();
        if(goodsService.deleteGoods(good.getSid())>0){
            result.setFlag(true);
            result.setData("删除成功");
        }else {
            result.setFlag(true);
            result.setData("该物品不存在");
        }
        return result;
    }

    //修改商品
    @ApiOperation("修改商品")
    @PostMapping("/updateGoods")
    @CacheEvict(value = "goodById",key = "#good")
    @PreAuthorize("hasAnyAuthority('supplier')")
    public Result updateGoods(@RequestBody Goods good){
        Result result = new Result();
        if (goodsService.updateById(good)){
            result.setFlag(true);
            result.setData("修改成功");
        }else {
            result.setFlag(false);
            result.setData("修改失败");
        }
        return result;
    }

    //根据类别查询商品(采购商)
    @ApiOperation("根据类别查询商品(采购商)")
    @PostMapping("/selectByCategoryC")
    @PreAuthorize("hasAnyAuthority('buyer')")
    public Result selectGoodsC(@RequestBody Goods good){

        Result result = new Result();

        List<Goods> goods = goodsService.selectByCategory(good.getClassification());
        if(goods.size()>0) {
            result.setData(goods);
            result.setFlag(true);
        }else{
            result.setFlag(true);
            result.setData("未有商品上架");
        }

        return result;
    }

    //根据商品名称和商品编号查询指定商品(采购商)
    @ApiOperation("根据商品名称和商品编号查询指定商品(采购商)")
    @PostMapping("/selectByIdAndGoodsC")
    @PreAuthorize("hasAnyAuthority('buyer')")
    public Result selectByIdC(@RequestBody Goods good){
        Result result = new Result();
        Goods good1 = goodsService.selectById(good);
        if(good1==null) {
            result.setFlag(true);
            result.setData("该商品未上架");
        }else {
            result.setFlag(true);
            result.setData(good1);
        }
        return result;
    }

    //查询所有商品(供应商)
    @ApiOperation("查询所有商品(供应商)")
    @PostMapping("/selectAllG")
    @PreAuthorize("hasAnyAuthority('supplier')")
    public Result selectAllG(@RequestBody Goods good){
        Result result = new Result();
        List<Goods> goods = goodsService.selectAllG(good.getUid());
        if(goods.size()>0){
            result.setFlag(true);
            result.setData(goods);
        }else {
            result.setFlag(true);
            result.setData("暂无上架商品");
        }
        return result;
    }

    //根据商品名称和类别查询商品(供应商)
    @ApiOperation("根据商品名称和类别查询商品(供应商)")
    @PostMapping("/selectByCategoryG")
    @PreAuthorize("hasAnyAuthority('supplier')")
    public Result selectByIdG(@RequestBody Goods good){
        Result result = new Result();
        
        Goods good2 = goodsService.selectByGoods(good);
        result.setFlag(true);
        if (good2 != null){
            result.setData(good2);
        }else {
            result.setData("暂无商品");
        }
        return result;
    }

    //根据id查询一个商品的信息
    @ApiOperation("根据id查询一个商品的信息")
    @PostMapping("/selectByOne")
    public Result selectByOne(@RequestBody Goods goods){
        Result result =new Result();
        Goods good = goodsService.getOne(new QueryWrapper<Goods>().eq("sid",goods.getSid()));

        if(good!=null){
            result.setFlag(true);
            result.setData(good);
        }else {
            result.setFlag(true);
            result.setData("无法展示信息");
        }

        return result;
    }
    
    
    @ApiOperation("分页查询接口")
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Goods goods){
        IPage<Goods> page = goodsService.getPage(currentPage, pageSize,goods);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()){
            page=goodsService.getPage( (int)page.getPages(), pageSize,goods);
        }
        return new Result(true,page);
    }

    @GetMapping("list")
    public Result findAll(@RequestBody PageHelper pageHelper){
        Result result = new Result();
        if (pageHelper !=null){
            int page= (pageHelper.getCurrentPage()-1)*pageHelper.getPageSize();
            pageHelper.setCurrentPage(page);
        }
        List<Goods> all = goodsService.findAll(pageHelper);
        List<Goods> count = goodsService.list();
        if (all !=null){
            result.setFlag(true);
            result.setMsg("查询成功^_^!");
            result.setData(all);
            result.setTotal(count.size());
        }else {
            result.setFlag(false);
            result.setMsg("查询失败~_~!");
        }
        return result;
    }
}
